package artid.com.arttogo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.fragment.Ambitus_Fragment;
import artid.com.arttogo.fragment.PersonMessage_Fragment;
import artid.com.arttogo.fragment.SystemMessage_Fragment;

public class PersonalMessageActivity extends BaseActivity implements
		OnClickListener {
	private RadioGroup mRadioGroup;
	private ViewPager mViewPager;
	private List<Fragment> fragementlist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personal_message);
		initView();
	}

	private void initView() {
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("消息");
		mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		// 得到RadioGroup中的RadioButton按钮
		mRadioGroup.getChildAt(0).setOnClickListener(this);
		mRadioGroup.getChildAt(1).setOnClickListener(this);
		mRadioGroup.getChildAt(2).setOnClickListener(this);
		mViewPager = (ViewPager) findViewById(R.id.PersonalMessage_pager);
		fragementlist = new ArrayList<Fragment>();
		SystemMessage_Fragment SystemMessage = new SystemMessage_Fragment();
		PersonMessage_Fragment PersonMessage = new PersonMessage_Fragment();
		Ambitus_Fragment Ambitus = new Ambitus_Fragment();
		fragementlist.add(SystemMessage);
		fragementlist.add(PersonMessage);
		fragementlist.add(Ambitus);
		mViewPager.setAdapter(new MyPageadapter(getSupportFragmentManager()));
		// 实现滑动 绑定监听（滑动页面并改变RadioButton的改变）
		mViewPager.setOnPageChangeListener(onPage);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
		case R.id.radio_news:
			// 系统消息
			mViewPager.setCurrentItem(0);
			break;
		case R.id.radio_me:
			// 个人消息
			mViewPager.setCurrentItem(1);
			break;
		case R.id.radio_ambitus:
			mViewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}

	// 实现滑动
	class MyPageadapter extends FragmentPagerAdapter {

		public MyPageadapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragementlist.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragementlist.size();
		}
	}

	OnPageChangeListener onPage = new OnPageChangeListener() {

		// 滑动页面并改变RadioButton的改变
		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			((RadioButton) mRadioGroup.getChildAt(arg0)).setChecked(true);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

	};

}
